/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RRHH_controlador;

import Conexion.Conexion;
import RRHH_Modelo.RHH_PERSONA;
import RRHH_Modelo.RHH_POSTULADO;
import RRHH_Modelo.RHH_POSTU_CONVO_APRO;
import RRHH_Modelo.RHH_USUARIO;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author equipo_4
 */
@WebServlet(name = "RRHH_CUADRO_COMPARATIVO_CONTROLLER", urlPatterns = {"/RRHH_CUADRO_COMPARATIVO_CONTROLLER"})
public class RRHH_CUADRO_COMPARATIVO_CONTROLLER extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try 
        {
            String evento = request.getParameter("evento");
            String html = "";
            RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
            Conexion con = new Conexion(usuario.getPASSWORD(), usuario.getPASSWORD());
                        
            switch(evento)
            {
                 case "buscarPostulados":
                    html=buscarPostulados(request, response,con);
                    break;
                 case "like":
                    html=like(request, response,con);
                    break;
            }
            
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(html);
            con.Close();
            
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(RRHH_CUADRO_COMPARATIVO_CONTROLLER.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String buscarPostulados(HttpServletRequest request, HttpServletResponse response, Conexion con) throws SQLException {
        int idconv = Integer.parseInt(request.getParameter("id"));
        String html="";
        List<RHH_POSTULADO> post = new RHH_POSTULADO(con).BuscarEnConvocatoria(idconv);
        
        RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
                
        for (int i = 0; i < post.size(); i++) 
        {
            RHH_PERSONA pers = new RHH_PERSONA(con);        
            RHH_POSTU_CONVO_APRO pca = new RHH_POSTU_CONVO_APRO(con);
            pca = pca.Buscar(post.get(i).getID(), idconv, usuario.getID());
            pers = pers.BuscarxID(post.get(i).getID_PERSONA());
            String foto="../img/imagen3.jpg";
            String checked="../img/menu/like.png";
            String checkedtext="ME GUSTA";
            if(pca!=null){
                checked="../img/menu/unlike.png";
                checkedtext=" ME GUSTA";
            }
            List<RHH_PERSONA> gustadores = pers.TodosLosQueAprobaron(post.get(i).getID(),idconv);
            String emplike="";
            for (int j = 0; j < gustadores.size(); j++) {
                emplike+="<span>"+gustadores.get(j).getPRIMER_NOMBRE()+" "+gustadores.get(j).getPRIMER_APELLIDO()+"</span><br>";
            }
            html+=  "<div style='display:inline-block; background: none repeat scroll 0% 0% white; padding: 10px; height: 120px; margin: 7px; border: thin solid; border-radius: 7px; width: 337px;'>\n" +
                    "<div style='float: left; height: 96px; padding: 7px; border: thin solid; width: 79px;'>\n" +
                        "<img src='"+foto+"' class='imagen' data-ci='" + pers.getCI()+"' data-foto='"+pers.getFOTO()+"' alt='' style='width: 100%; height: 100%;'/>\n" +
                    "</div>\n" +
                    "<div style='float: left; margin-left: 3px; padding: 5px; width: 191px; height: 91px;'>\n" +
                        "<div class='centrado negrilla'> "+
                            pers.getPRIMER_NOMBRE()+" "+pers.getPRIMER_APELLIDO() +" "+pers.getSEGUNDO_APELLIDO()+
                        "</div><br>\n" +
                        "<span class='negrilla'>DOCUMENTO DE IDENTIDAD:</span>\n" +
                            pers.getCI() +
                        "<br><span class='negrilla'>TELEFONO:</span>\n" +
                            pers.getTELEFONO() +
                        "<br><span class='negrilla'>CORREO:</span>\n" +
                            pers.getEMAIL() +
                    "</div>\n" +
                    "<div style='float: right; color: rgb(110, 127, 179);text-align: right; width: 126px;'>\n" +
                        "<span class='negrilla nrolike' data-like='"+emplike+"' \">"+gustadores.size()+"</span>\n" +
                        "<span class='negrilla'>"+checkedtext+"</span>\n" +
                        "<img src='"+checked+"' alt='' style='width: 20px;' data-estado='"+checkedtext+"' onclick=\"like('"+idconv+"','"+post.get(i).getID()+"',this,0)\"/>\n" +
                    "</div>\n" +
                    "<div style='color: rgb(110, 127, 179); padding: 5px; width: 86px; text-align: center; font-weight: bold;' onclick=\"vercurriculum('"+pers.getCI()+"','"+pers.getCURRICULUM()+"')\"> CURRICULUM </div>\n" +
                    "</div>";
        }
        return html;
    }

    private String like(HttpServletRequest request, HttpServletResponse response, Conexion con) throws SQLException {
        int idconv = Integer.parseInt(request.getParameter("idconv"));
        RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
        int postulado=Integer.parseInt(request.getParameter("postulado"));
        String tipo=request.getParameter("tipo");
        RHH_POSTU_CONVO_APRO pca = new RHH_POSTU_CONVO_APRO(con);
        pca = pca.Buscar(postulado, idconv, usuario.getID());
        if(tipo.equals("ME GUSTA"))
        {
            pca = new RHH_POSTU_CONVO_APRO(con);
            pca.setID_POSTULADO(postulado);
            pca.setID_CONVOCATORIA(idconv);
            pca.setID_USUARIO_LIKE(usuario.getID());
            pca.Insertar();
        }
        else
        {
            pca.Eliminar();
        }
        String emplike="";
        RHH_PERSONA pers = new RHH_PERSONA(con);        
        List<RHH_PERSONA> gustadores = pers.TodosLosQueAprobaron(postulado,idconv);
        for (int j = 0; j < gustadores.size(); j++) {
            emplike+="<span>"+gustadores.get(j).getPRIMER_NOMBRE()+" "+gustadores.get(j).getPRIMER_APELLIDO()+"</span><br>";
        }
        return "[\""+emplike+"\",\""+gustadores.size()+"\"]";
    }

}
